package com.yuan.kafkastudy.admin;

import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.CreateTopicsResult;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.common.internals.Topic;

import java.util.*;

public class AdminTopic {
    public static void main(String[] args) {
        Map<String, Object> configs = new HashMap<>();
        configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        //todo 管理员对象
        Admin admin = Admin.create(configs);
        //todo 构建主题需要三个参数
        //   第一个参数代表主题名称：字母、数字、点、下划线、中横线
        //   第二个参数代表主题分区数量
        //   第三个参数代表主题副本数量
        String topicName1 = "test_topic_1";
        int partition1 = 1;
        short replica1 = 1;
        NewTopic topic1 = new NewTopic(topicName1, partition1, replica1);

        String topicName2 = "test_topic_2";
        int partition2 = 2;
        short replica2 = 2;
        NewTopic topic2 = new NewTopic(topicName2, partition2, replica2);

        String topicName3 = "test_topic_3";
        Map<Integer, List<Integer>> map = new HashMap<>();
        map.put(0,Arrays.asList(3,1));
        map.put(1,Arrays.asList(2,3));
        map.put(2,Arrays.asList(1,3));
        NewTopic topic3 = new NewTopic(topicName3, map);

        //todo 创建主题
        CreateTopicsResult result = admin.createTopics(Arrays.asList(topic1, topic2));
        //todo 关闭admin
        admin.close();
    }
}
